package org.woosiyuan.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;

import javax.servlet.http.HttpServlet;

public class CarListService extends HttpServlet {

	public void init() {
		System.out.println("启动运行");
//		 saveCameraStream();
		
	}

	boolean mStopStream;

	String flag = "Content-Length: ";
	String flag1 = "\r\n";

	public void saveCameraStream() {
		URL aURL;
		FileOutputStream fileOutputStream = null;
		try {
			Calendar calendar = Calendar.getInstance();
			File file = new File("C:\\"+calendar.get(Calendar.YEAR)
					+ calendar.get(Calendar.MONTH)
					+ calendar.get(Calendar.DAY_OF_MONTH) + ".avi");
			fileOutputStream = new FileOutputStream(file, true);
			aURL = new URL(
					"http://192.168.1.106:81/videostream.cgi?user=admin&password=admin");
			URLConnection conn = aURL.openConnection();
			conn.connect();
			InputStream input = conn.getInputStream();
			System.out.println("input = " + input);
			if (input != null)
				mStopStream = false;
			int readLength = -1;
			int size = 0;
			 while (!mStopStream && size<10240000) {
			byte[] buffer = new byte[1024];
			readLength = input.read(buffer, 0, 1024);// readLength 本次读取数据的长度
			if (readLength > 0) {
				fileOutputStream.write(buffer);
				size += 1024;
				 }
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				fileOutputStream.flush();
				fileOutputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
